package cn.kepu.elearning.dto.match;

import cn.kepu.elearning.common.Constants;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Endy
 * @create 2019-12-12 上午9:12
 **/
@Getter
@Setter
@ToString(callSuper = true)
public class MatchQueryNewsDTO extends CommonQueryDTO {

    private String tenantId;
    private String ifTenantId;

    public MatchQueryDTO convert() {
        MatchQueryDTO matchQueryDTO = new MatchQueryDTO();
        matchQueryDTO.setType(Constants.Type.NEWS);
        matchQueryDTO.setPageNum(this.getPageNum());
        matchQueryDTO.setPageSize(this.getPageSize());
        matchQueryDTO.setFilterText(this.getFilterText());
        matchQueryDTO.setFieldNames(this.getFieldNames());
        matchQueryDTO.setOther(generatorOther());
        return matchQueryDTO;
    }

    private List<OtherQueryDTO> generatorOther() {
        List<OtherQueryDTO> other = new ArrayList<>();
        OtherQueryDTO other1 = new OtherQueryDTO();
        other1.setField("newsType");
        other1.setFieldType("number");
        other1.setValue("0");
        other1.setOperator(Constants.Operator.TERM);
        other1.setLogicOperator(Constants.LogicOperator.MUST);
        other.add(other1);

        OtherQueryDTO other2 = new OtherQueryDTO();
        other2.setField("status");
        other2.setFieldType("number");
        other2.setValue("1092");
        other2.setOperator(Constants.Operator.TERM);
        other2.setLogicOperator(Constants.LogicOperator.SHOULD);
        other.add(other2);
        OtherQueryDTO other3 = new OtherQueryDTO();
        other3.setField("recommendTag");
        other3.setFieldType("number");
        other3.setValue("1");
        other3.setOperator(Constants.Operator.TERM);
        other3.setLogicOperator(Constants.LogicOperator.SHOULD);
        other.add(other3);

        if (StringUtils.isNotBlank(this.getIfTenantId()) && StringUtils.isNotBlank(this.getTenantId())) {
            if ("2701".equals(this.getIfTenantId())) {
                OtherQueryDTO other5 = new OtherQueryDTO();
                other5.setField("tenantId");
                other5.setFieldType("number");
                other5.setValue(this.getTenantId());
                other5.setOperator(Constants.Operator.TERM);
                other5.setLogicOperator(Constants.LogicOperator.MUST);
                other.add(other5);
            } else if ("2702".equals(this.getIfTenantId())) {
                OtherQueryDTO other5 = new OtherQueryDTO();
                other5.setField("tenantId");
                other5.setFieldType("number");
                other5.setValue(this.getTenantId());
                other5.setOperator(Constants.Operator.TERM);
                other5.setLogicOperator(Constants.LogicOperator.MUST_NOT);
                other.add(other5);
            }
        }
        return other;
    }
}
